/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import com.sun.lwuit.Display;
import view.FunctionMenu;
import view.ViewManager;

/**
 *
 * @author Kvest
 */
public class Settings
{
    private static Settings settings;
    private static final Object lockObject = new Object();

    private RMSManager rmsManager;
    private SrverIPChanged ipChangedListener;
    private TakeOrderFlagChanged takeOrderFlagListener;
    
    private String nick;
    private String login;
    private String password;
    private boolean rememberPassword;
    private String serverIP;
    private boolean updateInterface;
    private boolean fullOrder;
    private boolean takeOrder;
    private boolean showPhone;
    private boolean showAllMassages;
    private String dispetcherCall;
    private String dispetcherSMS;
    private String newOrderMediaFile;
    private int newOrderMelodyIndex;
    private String yourOrderMediaFile;
    private int yourOrderMelodyIndex;
    private String messageMediaFile;
    private int messageMelodyIndex;
    private int volumeLevel;
    private boolean autoEnter;
    private String optionsPassword;
    private boolean switchOffMusic;
    private int voiceRington;
    private boolean switchLight;
    private boolean showVirtualKeyboard;
    private boolean saveLogs;
    private long lastUpdate;
    private int autoTariffClassUID;
    //Бесплатное время ожидания
    private int freeWaitTime;
    //Поправка на время
    private int timeFixer;
    private int maxArchivOrdersCount;
    private int orderTextHeight;

    private int preordersUpdatePeriod; 
    private int efirMusicReplayPeriod;
    
    private Settings()
    {
        int menuType = ViewManager.getViewManager().getFunctionMenuType();
        nick = "";
        login = "";
        password = "";
        rememberPassword = true;
        serverIP = "";
        updateInterface = true;
        fullOrder = false;
        takeOrder = true;
        showPhone = true;
        showAllMassages = false;
        dispetcherCall = "";
        dispetcherSMS = "";
        newOrderMediaFile = "";
        newOrderMelodyIndex = 0;
        yourOrderMediaFile = "";
        yourOrderMelodyIndex = 0;
        messageMediaFile = "";
        messageMelodyIndex = 0;
        volumeLevel = 5;
        autoEnter = false;
        optionsPassword = "";
        switchOffMusic = false;
        voiceRington = 0;
        switchLight = false;
        showVirtualKeyboard = true;
        saveLogs = false;
        freeWaitTime = 5;
        timeFixer = 0;
        if (menuType == FunctionMenu.CHERNOVTSI_TYPE)
        {
            preordersUpdatePeriod = 1;
            efirMusicReplayPeriod = 12;
        }
        else
        {
            preordersUpdatePeriod = 0;
            efirMusicReplayPeriod = 0;
        }
        maxArchivOrdersCount = 20;
        orderTextHeight = 23;
        
        rmsManager = new RMSManager("optionsDB");

        load();
    }

    public static Settings getSettings()
    {
        synchronized (lockObject)
        {
            if (settings == null)
            {
                settings = new Settings();
            }
        }

        return settings;
    }

    public final void load()
    {
        serverIP = rmsManager.load("ip");
        nick = rmsManager.load("nick");
        login = rmsManager.load("login");
        String tmp = rmsManager.load("rem");
        if (!tmp.equals(""))
        {
            rememberPassword = (tmp.equals("1"));
        }
        if (rememberPassword)
	{
            password = rmsManager.load("password");
        }
        fullOrder = rmsManager.load("fullOrder").equals("1");
        tmp = rmsManager.load("updateInterface");
        if (!tmp.equals(""))
        {
            updateInterface = (tmp.equals("1"));
        }
        tmp = rmsManager.load("takeorder");
        if (!tmp.equals(""))
        {
            takeOrder = (tmp.equals("1")); 
        }
        tmp = rmsManager.load("showphone");
        showPhone= (tmp.equals("1")|| tmp.equals(""));
        showAllMassages = rmsManager.load("showallmsg").equals("1");
        dispetcherCall = rmsManager.load("dispcall");
        dispetcherSMS = rmsManager.load("dispsms");
        newOrderMediaFile = rmsManager.load("newOrderFile");
        yourOrderMediaFile = rmsManager.load("yourOrderFile");
	messageMediaFile = rmsManager.load("messageFile");
        try
        {
            volumeLevel = Integer.parseInt(rmsManager.load("volume"));
        }
        catch(Exception e)
        {
            volumeLevel=5;
        }
        optionsPassword = rmsManager.load("optpassword");
        if (optionsPassword.equals(""))
        {
            if (ViewManager.getViewManager().getFunctionMenuType() == FunctionMenu.CHERNOVTSI_TYPE)
            {
                optionsPassword = "98a4df8704bf628ef3d8b1369f0fdc78";//"848cd0ac9908662ad7f42b91e9ab5b9a";
            }
            else
            {
                optionsPassword = "e10adc3949ba59abbe56e057f20f883e";
            }
        }
        autoEnter = rmsManager.load("autoenter").equals("1");
        switchOffMusic = rmsManager.load("switchOffMusic").equals("1");
        try
        {
            voiceRington = Integer.parseInt(rmsManager.load("voiceRington"));
        }
        catch (Exception e)
        {
            voiceRington = 0;
        }
        if (!rmsManager.load("newOrderMelodyIndex").equals(""))
	{
            try
            {
                newOrderMelodyIndex = Integer.parseInt(rmsManager.load("newOrderMelodyIndex"));
            }catch(Exception e){}
	}
        if (!rmsManager.load("yourOrderMelodyIndex").equals(""))
	{
            try
            {
                yourOrderMelodyIndex = Integer.parseInt(rmsManager.load("yourOrderMelodyIndex"));
            }catch(Exception e){}
	}
        if (!rmsManager.load("messageMelodyIndex").equals(""))
	{
            try
            {
                messageMelodyIndex = Integer.parseInt(rmsManager.load("messageMelodyIndex"));
            }catch(Exception e){}
	}
        saveLogs = rmsManager.load("savelogs").equals("1");
        try
        {
            lastUpdate = Long.parseLong(rmsManager.load("lastUp"));
	}
        catch(Exception e)
        {
            lastUpdate = 0;
        }
        try
        {
            autoTariffClassUID = Integer.parseInt(rmsManager.load("atcuid"));
        }
        catch(Exception e)
        {
            autoTariffClassUID = 0;
        }
        
        MainOne.LOG_STR = rmsManager.load("logstr");

        try
        {
            freeWaitTime = Integer.parseInt(rmsManager.load("f_w_t"));
        }
        catch(Exception ex){}
        try
        {
            timeFixer = Integer.parseInt(rmsManager.load("time_fix"));
        }
        catch(Exception ex){}
        try
        {
            preordersUpdatePeriod = Integer.parseInt(rmsManager.load("preord_upd_per"));
        }
        catch(Exception ex){}
        try
        {
            efirMusicReplayPeriod = Integer.parseInt(rmsManager.load("efir_music_replay"));
        }
        catch(Exception ex){}
        
        try {
            maxArchivOrdersCount = Integer.parseInt(rmsManager.load("maoc"));
        } catch(Exception ex){}
        try {
            orderTextHeight = Integer.parseInt(rmsManager.load("oth"));
        } catch(Exception ex){}
        
        tmp = rmsManager.load("showvk");
        setShowVirtualKeyboard((tmp.equals("1")|| tmp.equals("")));
        tmp = rmsManager.load("light");
        switchLight = (tmp.equals("1"));
    }

    public void saveOptions()
    {
         //Автовход
        if (autoEnter)
        {
            rmsManager.save("autoenter=1","autoenter");
        }
        else
        {
            rmsManager.save("autoenter=0","autoenter");
        }
        //Настройки по мелодиям
        //Отключение всего звука
	if (switchOffMusic)
	{
            rmsManager.save("switchOffMusic=1","switchOffMusic");
	}
	else
	{
            rmsManager.save("switchOffMusic=0","switchOffMusic");
	}
	//Голосовой сигнал
        rmsManager.save("voiceRington=" + Integer.toString(voiceRington), "voiceRington");
        //Настройки мелодии для нового заказа
        rmsManager.save("newOrderFile=" + newOrderMediaFile,"newOrderFile");
	rmsManager.save("newOrderMelodyIndex=" + Integer.toString(newOrderMelodyIndex),"newOrderMelodyIndex");
        //Настройки мелодии на получение заказа
        rmsManager.save("yourOrderFile=" + yourOrderMediaFile,"yourOrderFile");
        rmsManager.save("yourOrderMelodyIndex=" + Integer.toString(yourOrderMelodyIndex),"yourOrderMelodyIndex");
        //Настройки мелодии на получение сообщения
        rmsManager.save("messageFile=" + messageMediaFile,"messageFile");
	rmsManager.save("messageMelodyIndex=" + Integer.toString(messageMelodyIndex),"messageMelodyIndex");
        //Громкость
        rmsManager.save("volume=" + volumeLevel, "volume");
        //Сохранять ли лог
        if (saveLogs)
        {
            rmsManager.save("savelogs=1","savelogs");
        }
        else
        {
            rmsManager.save("savelogs=0","savelogs");
        }
        if (showVirtualKeyboard)
        {
            rmsManager.save("showvk=1", "showvk");
        }
        else
        {
            rmsManager.save("showvk=0", "showvk");
        }
        if (switchLight)
        {
            rmsManager.save("light=1", "light");
        }
        else
        {
            rmsManager.save("light=0", "light");
        }
        //Коррекция времени
        rmsManager.save("time_fix=" + timeFixer,"time_fix");
        
        rmsManager.save("preord_upd_per=" + preordersUpdatePeriod, "preord_upd_per");
        rmsManager.save("efir_music_replay=" + efirMusicReplayPeriod, "efir_music_replay");
        rmsManager.save("maoc=" + maxArchivOrdersCount , "maoc");
        rmsManager.save("oth=" + orderTextHeight, "oth");
    }

    public void saveSettings()
    {
        rmsManager.save("nick=".concat(nick),"nick");
        rmsManager.save("login=".concat(login),"login");
	if (rememberPassword)
	{
            rmsManager.save("rem=1","rem");
            rmsManager.save("password=".concat(password),"password");
	}
	else
	{
            rmsManager.save("rem=0","rem");
	}
        rmsManager.save("ip=".concat(serverIP),"ip");

         //Автообновление интерфейса
	if (updateInterface)
	{
            rmsManager.save("updateInterface=1","updateInterface");
	}
	else
	{
            rmsManager.save("updateInterface=0","updateInterface");
	}
        //Полный заказ
	if (fullOrder)
	{
            rmsManager.save("fullOrder=1","fullOrder");
	}
	else
	{
            rmsManager.save("fullOrder=0","fullOrder");
	}

        //"Брать заказы"
        if (takeOrder)
        {
            rmsManager.save("takeorder=1","takeorder");
        }
        else
        {
            rmsManager.save("takeorder=0","takeorder");
        }
        //Показывать номер клиента
        if (showPhone)
        {
            rmsManager.save("showphone=1","showphone");
        }
        else
        {
            rmsManager.save("showphone=0","showphone");
        }

        if (showAllMassages)
        {
            rmsManager.save("showallmsg=1","showallmsg");
        }
        else
        {
            rmsManager.save("showallmsg=0","showallmsg");
        }
        //Телефон диспетчера для звонков
        rmsManager.save("dispcall=".concat(dispetcherCall),"dispcall");
        //Телефон диспетчера для смс
        rmsManager.save("dispsms=".concat(dispetcherSMS),"dispsms");
        //Бесплатное время ожидания
        rmsManager.save("f_w_t=" + freeWaitTime, "f_w_t");
    }

    public void save()
    {
        saveSettings();
        saveOptions();
    }

    public void setIpChangedListener(SrverIPChanged ipChangedListener)
    {
        this.ipChangedListener = ipChangedListener;
    }

    public void setTakeOrderFlagListener(TakeOrderFlagChanged takeOrderFlagListener)
    {
        this.takeOrderFlagListener = takeOrderFlagListener;
    }

    public String getNick()
    {
        return nick;
    }

    public void setNick(String value)
    {
        nick = value;
    }

    public String getLogin()
    {
        return login;
    }

    public void setLogin(String value)
    {
        login = value;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public boolean isRememberPassword()
    {
        return rememberPassword;
    }

    public void setRememberPassword(boolean rememberPassword)
    {
        this.rememberPassword = rememberPassword;
    }

    public String getServerIP()
    {
        return serverIP;
    }

    public void setServerIP(String serverIP)
    {
        if (!this.serverIP.equals(serverIP) && ipChangedListener != null)
        {
            ipChangedListener.ipChanged(this.serverIP, serverIP);
        }

        this.serverIP = serverIP;
    }

    public boolean isUpdateInterface()
    {
        return updateInterface;
    }

    public void setUpdateInterface(boolean updateInterface)
    {
        this.updateInterface = updateInterface;
    }

    public boolean isFullOrder()
    {
        return fullOrder;
    }

    public void setFullOrder(boolean fullOrder)
    {
        this.fullOrder = fullOrder;
    }

    public boolean isTakeOrder()
    {
        return takeOrder;
    }

    public void setTakeOrder(boolean takeOrder)
    {
        if (this.takeOrder != takeOrder && takeOrderFlagListener != null)
        {
            takeOrderFlagListener.takeOrderFlagChanged(takeOrder);
        }
        this.takeOrder = takeOrder;
    }

    public boolean isShowPhone()
    {
        return showPhone;
    }

    public void setShowPhone(boolean showPhone)
    {
        this.showPhone = showPhone;
    }

    public boolean isShowAllMassages()
    {
        return showAllMassages;
    }

    public void setShowAllMassages(boolean showAllMassages)
    {
        this.showAllMassages = showAllMassages;
    }

    public String getDispetcherCall()
    {
        return dispetcherCall;
    }

    public void setDispetcherCall(String dispetcherCall)
    {
        this.dispetcherCall = dispetcherCall;
    }

    public String getDispetcherSMS()
    {
        return dispetcherSMS;
    }

    public void setDispetcherSMS(String dispetcherSMS)
    {
        this.dispetcherSMS = dispetcherSMS;
    }

    public String getMessageMediaFile()
    {
        return messageMediaFile;
    }

    public void setMessageMediaFile(String messageMediaFile)
    {
        this.messageMediaFile = messageMediaFile;
    }

    public String getNewOrderMediaFile()
    {
        return newOrderMediaFile;
    }

    public void setNewOrderMediaFile(String newOrderMediaFile)
    {
        this.newOrderMediaFile = newOrderMediaFile;
    }

    public int getVolumeLevel()
    {
        return volumeLevel;
    }

    public void setVolumeLevel(int volumeLevel)
    {
        this.volumeLevel = volumeLevel;
    }

    public String getYourOrderMediaFile()
    {
        return yourOrderMediaFile;
    }

    public void setYourOrderMediaFile(String yourOrderMediaFile)
    {
        this.yourOrderMediaFile = yourOrderMediaFile;
    }

    public boolean isAutoEnter()
    {
        return autoEnter;
    }

    public void setAutoEnter(boolean autoEnter)
    {
        this.autoEnter = autoEnter;
    }

    public String getOptionsPassword()
    {
        return optionsPassword;
    }

    public void setOptionsPassword(String optionsPassword)
    {
        this.optionsPassword = optionsPassword;
    }

    public boolean isSwitchOffMusic()
    {
        return switchOffMusic;
    }

    public void setSwitchOffMusic(boolean switchOffMusic)
    {
        this.switchOffMusic = switchOffMusic;
    }

    public int getVoiceRington()
    {
        return voiceRington;
    }

    public void setVoiceRington(int voiceRington)
    {
        this.voiceRington = voiceRington;
    }

    public int getNewOrderMelodyIndex()
    {
        return newOrderMelodyIndex;
    }

    public void setNewOrderMelodyIndex(int newOrderMelodyIndex)
    {
        this.newOrderMelodyIndex = newOrderMelodyIndex;
    }

    public int getYourOrderMelodyIndex()
    {
        return yourOrderMelodyIndex;
    }

    public void setYourOrderMelodyIndex(int yourOrderMelodyIndex)
    {
        this.yourOrderMelodyIndex = yourOrderMelodyIndex;
    }

    public int getMessageMelodyIndex()
    {
        return messageMelodyIndex;
    }

    public void setMessageMelodyIndex(int messageMelodyIndex)
    {
        this.messageMelodyIndex = messageMelodyIndex;
    }

    public boolean isSaveLogs()
    {
        return saveLogs;
    }

    public void setSaveLogs(boolean saveLogs)
    {
        this.saveLogs = saveLogs;
    }
    
    public String getFilter()
    {
        return rmsManager.load("filter");
    }

    public void setFilter(String value)
    {
        rmsManager.save("filter=" + value, "filter");
    }

    public long getLastUpdate()
    {
        return lastUpdate;
    }

    public void setLastUpdate(long lastUpdate)
    {
        this.lastUpdate = lastUpdate;
        rmsManager.save("lastUp=".concat(Long.toString(lastUpdate)),"lastUp");
    }

    public int getAutoTariffClassUID() 
    {
        return autoTariffClassUID;
    }

    public void setAutoTariffClassUID(int autoTariffClassUID) 
    {
        this.autoTariffClassUID = autoTariffClassUID;
        rmsManager.save("atcuid=".concat(Integer.toString(autoTariffClassUID)),"atcuid");
    }

    public void saveLog()
    {
        if (saveLogs)
        {
            rmsManager.save("logstr=" + MainOne.LOG_STR, "logstr");
        }
    }

    public boolean isSwitchLight() 
    {
        return switchLight;
    }

    public void setSwitchLight(boolean switchLight) 
    {
        this.switchLight = switchLight;
    }
    
    public boolean isShowVirtualKeyboard()
    {
        return showVirtualKeyboard;
    }

    public void setShowVirtualKeyboard(boolean showVirtualKeyboard)
    {
        this.showVirtualKeyboard = showVirtualKeyboard;
        Display.getInstance().setShowVirtualKeyboard(this.showVirtualKeyboard);
    }

    public int getFreeWaitTime()
    {
        return freeWaitTime;
    }

    public void setFreeWaitTime(int freeWaitTime)
    {
        this.freeWaitTime = freeWaitTime;
    }

    public int getTimeFixer()
    {
        return timeFixer;
    }

    public void setTimeFixer(int timeFixer)
    {
        this.timeFixer = timeFixer;
        TimerService.getTimerService().setTimeFixer(this.timeFixer);
    }

    public int getPreordersUpdatePeriod() 
    {
        return preordersUpdatePeriod;
    }

    public void setPreordersUpdatePeriod(int preordersUpdatePeriod) 
    {
        this.preordersUpdatePeriod = preordersUpdatePeriod;
    }

    public int getEfirMusicReplayPeriod() 
    {
        return efirMusicReplayPeriod;
    }

    public void setEfirMusicReplayPeriod(int efirMusicReplayPeriod) 
    {  
        this.efirMusicReplayPeriod = efirMusicReplayPeriod;
    }

    public int getMaxArchivOrdersCount() {
        return maxArchivOrdersCount;
    }

    public void setMaxArchivOrdersCount(int maxArchivOrdersCount) {
        this.maxArchivOrdersCount = maxArchivOrdersCount;
    }

    public int getOrderTextHeight() {
        return orderTextHeight;
    }

    public void setOrderTextHeight(int orderTextHeight) {
        this.orderTextHeight = orderTextHeight;
    }
}
